package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.atag.dao.ext.HibernateExtendDAO;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import com.bsoft.gol.hcs.specialist.entity.FollowUpPathRouteEntity;

import java.util.List;

/**
 * @author hekr22831
 * @description 患者院后随访设置
 * @date 2022/5/25.21:50
 */
public abstract class FollowUpPathRouteDAO extends HibernateExtendDAO<FollowUpPathRouteEntity> {
        //HibernateSupportDelegateDAO<FollowUpPathRouteEntity> {
    public FollowUpPathRouteDAO() {
        this.setKeyField("fusPathRouteId");
    }

    /**
     * 查询需要随访且没有生成随访计划的医生站设置信息
     *
     * @return java.util.List<specialist.entity.FollowUpPathRouteEntity>
     * @throws
     * @author tongtp
     * @date 2022/6/8 10:44
     */
    @DAOMethod(sql = "from FollowUpPathRoute where followUpType = 1 and status = '0' ")
    public abstract List<FollowUpPathRouteEntity> queryNeedProduceSetting();

    /**
     * 查询患者院后随访设置
     *
     * @param organizationId 机构代码
     * @param visitNo        住院号或就诊序号
     * @param source         类别
     * @return
     */
    @DAOMethod(sql = "from FollowUpPathRoute where organizationId = :organizationId and visitNo=:visitNo and " +
            "source=:source")
    public abstract FollowUpPathRouteEntity getFuprByCardNoAndSource(@DAOParam("organizationId") String organizationId, @DAOParam("visitNo") String visitNo, @DAOParam("source") Byte source);

    /**
     * 查询患者院后随访设置
     * @param fusPathRouteId 院后随访id
     * @return
     */
    @DAOMethod(sql = "from FollowUpPathRoute where fusPathRouteId=:fusPathRouteId")
    public abstract FollowUpPathRouteEntity findFuprByFusPathRouteId(@DAOParam("fusPathRouteId") long fusPathRouteId);

    /**
     * 根据fusPathRouteId更新后院随访设置表
     * @param fupr
     */
    public void updateByFusPathRouteId(final FollowUpPathRouteEntity fupr) {
//    public void updateById(final FollowUpPathRouteEntity fupr) {
        HibernateStatelessResultAction action = new AbstractHibernateStatelessResultAction() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                Query query = ss.createQuery("update FollowUpPathRoute p set " +
                        "p.routeId=:routeId,p.routeName=:routeName,p.followUpType=:followUpType,p.comment=:comment," +
                        "p.visitNo=:visitNo,p.source=:source,p.createDt=:createDt,p.modifiedDt=:modifiedDt " +
                        "where p.fusPathRouteId=:fusPathRouteId");
                query.setParameter("fusPathRouteId", fupr.getFusPathRouteId());
                query.setParameter("routeId", fupr.getRouteId());
                query.setParameter("routeName", fupr.getRouteName());
                query.setParameter("followUpType", fupr.getFollowUpType());
                query.setParameter("comment", fupr.getComment());
                query.setParameter("visitNo", fupr.getVisitNo());
                query.setParameter("source", fupr.getSource());
                query.setParameter("createDt", fupr.getCreateDt());
                query.setParameter("modifiedDt", fupr.getModifiedDt());
                query.executeUpdate();
            }
        };
        HibernateSessionTemplate.instance().execute(action);
    }
}
